<?
require("../../common/init.php");
require("author.php");
require("upfile.php");
include(ROOT."common/class_image_resize.php");

$shipment_method=new DB(Tshipment_method,$conn);
$sales_order=new DB(Tsales_order,$conn);
$sales_order_detail=new DB(Tsales_order_detail,$conn);
$express_company=new DB(Texpress_company,$conn);
$gee=$sales_order; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象
$refund = new DB(Trefund, $conn);
//$cate=$product_category; // cate = category, 分类

$filter="1";
if(isset($_GET["filter"]))$filter=clean($_GET["filter"]);
if(isset($_POST["filter"]))$filter=clean($_POST["filter"]);

switch ($filter){
    case 1:
        $title=ROOT_PENDING_PAYMENT_ORDER_LIST;
        break;
    case 2:
        $title=ROOT_PAID_ORDER_LIST;
        break;
    case 3:
        $title=ROOT_PENDING_SHIPMENT_ORDER_LIST;
        break;
	case 4:
        $title=ROOT_SHIPPED_ORDER_LIST;
        break;
	case 5:
        $title=ROOT_DELIVERED_ORDER_LIST;
        break;
	case 6:
        $title=ROOT_REFUNDED_ORDER_LIST;
        break;
	default:
		$title=ROOT_ORDER_LIST;
}

$url='sales_order';
$s=isset($_GET["s"])?clean($_GET["s"]):''; // search
$cateid=isset($_GET["cid"])?clean($_GET["cid"]):0;
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除 
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD 
$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"]; // current page 当前页码
$id=0; if (isset($_GET["id"])) $id = clean($_GET["id"]);

// sql action / sql 动作
switch ($action){
	case "update":
		sql_update();
		break;
	case "add":
		sql_insert();
		break;
}

if($id==0){
	$order='';
	$gee->rows=15;
	$where=array();
	if($filter!=''){
		$where[]=array('name'=>'order_status','val'=>$filter,'opn'=>'=', 'and'=>'and');
	}
	if($s!='') {
		$where[]=array('name'=>'ref_no','val'=>$s,'opn'=>'like', 'and'=>'and', 'start'=>'(');
		$where[]=array('name'=>'mobile','val'=>$s,'opn'=>'like', 'and'=>'or');
		$where[]=array('name'=>'phone','val'=>$s,'opn'=>'like', 'and'=>'or');
		$where[]=array('name'=>'contact_name','val'=>$s,'opn'=>'like', 'and'=>'or', 'end'=>')');
	}
	$pages=$gee->count_pages($where);
	$info=$gee->get_page($page,$where);
}
else $info=$gee->get_row($id);

function update($info)
{
	global $gee, $url, $h, $id;
	if($id==0) 
	{
		echo "Invali argument!!!";
		exit;
	}
	echo $h->form($url.'.php', 1, '');
  	show_box($info,true, false);
	echo $h->hidden('action', 'update');
	echo $h->former();
}
function delete($id)
{
	global $gee,$url;
	if($dave->delete($uid)) wheel($url.'.php');
}	
function show_info()
{
	global $gee,$info,$id;
	if($id==0) show_boxes($info);
	else show_box($info, false, false);
}
function sql_update()
{
	global $product, $gee, $url, $filter, $customer;
	$picdir=ROOT.'upload/';
	$id=iso(@$_POST["id"]);
	$info=$gee->get_row($id); // order/订单
	$candelete=0;
	$arr=array(
		'shipped_time'=>iso(@$_POST["shipped_time"]),
		'express'=>iso(@$_POST["express"]),
		'shipment_tracking_no'=>iso(@$_POST["shipment_tracking_no"]),
		'order_status'=>iso(@$_POST["order_status"]),
		'operator_remark'=>iso(@$_POST["operator_remark"]),
		);
	if( isset($_FILES["cpic"]) && $_FILES["cpic"]["size"] > 0) {
		$oldcpic=$picdir.$info['receipt_doc'];
		$candelete=1;

		$pic=upfile($_FILES["cpic"]);
		$image_resize=new Image_resize($picdir.$pic,600,600,0,$picdir.$pic);
		$arr['receipt_doc']=$pic;
	}
	if(isset($_POST['paid_time'])) $arr['paid_time']=$_POST['paid_time'];
	if(isset($_POST['payer'])) $arr['payer']=$_POST['payer'];
	if(isset($_POST['payer_id'])) $arr['payer_id']=$_POST['payer_id'];
	if(isset($_POST['payment_ref_no'])) $arr['payment_ref_no']=$_POST['payment_ref_no'];

	if($gee->update($arr, 'id', $id)){
		if($_POST['order_status'] == 5){
			//if($_POST['payment_method_id'] == 5 || $_POST['payment_method_id'] == 6){
	            // 加积分 add point
	            $customer->add_point_by_order($info['ref_no']);
	            // 更新级别
				$customer->update_grade($info['customer_id']);
			//}
		}
		// 是否删除旧的收款凭据
		if($candelete)@unlink($oldcpic);
		
		// 货到付款/银行转账的订单, 管理员取消后要恢复库存
		$ref_no=$info['ref_no'];
		$payment_method_id=$info['payment_method_id'];
		if($arr['order_status']=='9'){
			if($payment_method_id==5 || $payment_method_id==6)
			$product->restore_stock_by_order($ref_no);
		}
		yell(ROOT_HANDLE_SUCCESS,$url.'.php'.(($filter!="")?"?page=".$page."&filter=".$filter:""));
	}
	else echo mysql_error();
}

// view 加载视图
require ROOTVIEW.$url.'.php';

// end sales_order.php